package com.yanqu.road.dao.impl.activity.selectlottery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.selectlottery.data.SelectLotteryUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SelectLotteryUserDataDaoImpl extends TempDataObjectDao<SelectLotteryUserData> {


    public static String getSelectLotteryUserDataSql = "select * from `t_u_activity_select_lottery_user_data` where `userId`=?";
    public Map<Integer, SelectLotteryUserData> getSelectLotteryUserDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, SelectLotteryUserData> userDataMap = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSelectLotteryUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                SelectLotteryUserData userData = parse(rs);
                userDataMap.put(userData.getActivityId(), userData);
            }
            return userDataMap;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateSelectLotteryUserDataSql = "update `t_u_activity_select_lottery_user_data` set selectRewardId=?,drawNum=?,seRewardNum=?,allDrawNum=?,lastEnterTime=?,selectNumMap=?,createTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SelectLotteryUserData selectLotteryUserData) {
        selectLotteryUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getSelectRewardId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getDrawNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getSeRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getAllDrawNum()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getLastEnterTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(selectLotteryUserData.getSelectNumMap())));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSelectLotteryUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSelectLotteryUserDataSql = "insert into `t_u_activity_select_lottery_user_data`(activityId,userId,selectRewardId,drawNum,seRewardNum,allDrawNum,lastEnterTime,selectNumMap,createTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SelectLotteryUserData selectLotteryUserData) {
        selectLotteryUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getSelectRewardId()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getDrawNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getSeRewardNum()));
        para.put(i++, new DbParameter(Types.INTEGER, selectLotteryUserData.getAllDrawNum()));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getLastEnterTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(selectLotteryUserData.getSelectNumMap())));
        para.put(i++, new DbParameter(Types.BIGINT, selectLotteryUserData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSelectLotteryUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SelectLotteryUserData parseData(ResultSet rs) throws SQLException {
        SelectLotteryUserData selectLotteryUserData = new SelectLotteryUserData();
        selectLotteryUserData.setActivityId(rs.getInt("activityId"));
        selectLotteryUserData.setUserId(rs.getLong("userId"));
        selectLotteryUserData.setSelectRewardId(rs.getInt("selectRewardId"));
        selectLotteryUserData.setDrawNum(rs.getInt("drawNum"));
        selectLotteryUserData.setSeRewardNum(rs.getInt("seRewardNum"));
        selectLotteryUserData.setAllDrawNum(rs.getInt("allDrawNum"));
        selectLotteryUserData.setLastEnterTime(rs.getLong("lastEnterTime"));
        if (!StringUtils.isNullOrEmpty(rs.getString("selectNumMap"))) {
            selectLotteryUserData.setSelectNumMap(JSON.parseObject(rs.getString("selectNumMap"), new TypeReference<Map<Integer, Integer>>() {}));
        }
        selectLotteryUserData.setCreateTime(rs.getLong("createTime"));
        return selectLotteryUserData;
    }
}
